package com.example.mvvmdemo;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
//管理UI Data
public class MainViewModel extends /*ViewModel*/ AndroidViewModel {
    //UI Data
    //传统的方式 数据
//    private String phoneInfo = "";

    //LiveData 感应改变数据
    private MutableLiveData<String> phoneInfo;

    private Context context;

    public MainViewModel(@NonNull Application application) {
        super(application);
        context = application;
    }

    public MutableLiveData<String> getPhoneInfo(){
        if (phoneInfo == null){
            phoneInfo = new MutableLiveData<>();

            phoneInfo.setValue("1524353");//默认值
        }
        return phoneInfo;
    }
    public void appendNumber(String number){
        phoneInfo.setValue(phoneInfo.getValue() + number);
    }

    public void backspaceNumber(){
        int length = phoneInfo.getValue().length();
        if (length > 0){
            phoneInfo.setValue(phoneInfo.getValue().substring(0,phoneInfo.getValue().length() - 1));
        }
    }
    public void callPhone(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phoneInfo.getValue()));
        context.startActivity(intent);
    }
}
